package com.dm.saleassback.categorymanage.service.impl;

import com.dm.saleassback.categorymanage.mapper.CategoryMapper;
import com.dm.saleassback.categorymanage.service.CategoryService;
import com.dm.saleassback.common.util.EncryptionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * @Description 查询类别
     * @Author zhangxue
     * @Date 16:54 2018/5/31
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllCategory(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = categoryMapper.queryAllCategoryMapper(param);
        Map<String,String> countMap = categoryMapper.queryCategoryCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        JSONArray ja = JSONArray.fromObject(resultList);
        resultMap.put("data",ja.toString());
        resultMap.put("totalnum",totalSize);
        return resultMap;
    }

    /**
         * @Description 新增类别信息
         * @Author zhangxue
         * @Date 15:00 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveCategory(Map<String, Object> param) {
        //查询所有类别
        List<Map<String,Object>> cateList = new ArrayList<Map<String, Object>>();
        cateList = categoryMapper.queryAllCategory(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(cateList.size()>=1){
            resultMap.put("status","0");
            return resultMap;
        }
        int i = categoryMapper.saveCategoryMapper(param);
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 删除类别信息
         * @Author zhangxue
         * @Date 15:07 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> deleteCategoryById(Map<String, Object> param) {
        int i = categoryMapper.deleteCategoryById(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 修改类别信息
         * @Author zhangxue
         * @Date 15:10 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> updateCategoryById(Map<String, Object> param) {
        int i = categoryMapper.updateCategoryById(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

}
